package test;

import com.config.DBConfig;
import com.dao.AdminDao;
import com.dao.ReceiveAddressDao;
import com.entity.Admin;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
import org.junit.Test;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class Demo1 {
    @Test
    public void test7() {
        ManagedTransactionFactory factory = new ManagedTransactionFactory();
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://rm-wz9okfspb8atf5s716o.mysql.rds.aliyuncs.com:3306/mall?useSSL=false&useUnicode=true&autoReconnect=true&rewriteBatchedStatements=true");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setPassword("Zhangpingyang@123");
        dataSource.setUsername("root");
        Environment dev = new Environment("dev", factory, dataSource);
        Configuration configuration = new Configuration(dev);
        configuration.addMapper(ReceiveAddressDao.class);
//        configuration.addMapper(AdminDao.class);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ReceiveAddressDao mapper = sqlSession.getMapper(ReceiveAddressDao.class);
//        AdminDao adminMapper = sqlSession.getMapper(AdminDao.class);
//        List<Map<String, Object>> list = mapper.getList();
        Map<String, Object> one = mapper.getOne(1);
        System.out.println(one);
//        List<Admin> adminList = adminMapper.getAdminList();
//        System.out.println(adminList);

    }

    @Test
    public void test9() {
        boolean flag = false;
        try {
            if (1 == 1)
                throw new RuntimeException();
            flag = true;
        }finally {
            System.out.println(flag);
        }
    }
    /*method.isBridge
    * 父类中使用泛型 子类泛型为具体的泛型， 则会生成一个bridge方法
    **/
    @Test
    public void test8() {
        Class<ChildClass> childClassClass = ChildClass.class;
        Method[] declaredMethods = childClassClass.getDeclaredMethods();
        System.out.println(declaredMethods.length);
    }
    private interface SupClass<T>{
        void method(T t);
    }

    private class ChildClass implements SupClass<String> {
        @Override
        public void method(String t) {

        }
    }
    /*method.isBridge */
    @Test
    public void test6() {
        char c = '㎡';
//        System.out.println((int)c);
        String pattern = "^[\\w\\~\\!\\@\\$\\^\\*\\(\\)\\_\\`\\-\\[\\]\\\\\\|\\;\\'\\,\\.\\:\\\"\\<\\>]+$";
        System.out.println(Pattern.matches(pattern, "SN-30"));
        System.out.println(Pattern.matches(pattern, "SN-09-2"));
        System.out.println(Pattern.matches(pattern, "SN-09-1"));
        System.out.println(Pattern.matches(pattern, "3*1.5㎡+1㎡"));
        System.out.println(Pattern.matches(pattern, "7.5KW (10HP) 380V"));
    }
    @Test
    public void test5() {
//        DefaultSqlSession session = new DefaultSqlSession();

    }
    @Test
    public void test1() {
        Annotation[] declaredAnnotations = DBConfig.class.getDeclaredAnnotations();
        System.out.println();
    }

    @Test
    public void test2() {
        Method[] declaredMethods = XxxDao.class.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method.getName() + "  " + method.isDefault());
        }
        Method[] declaredMethods1 = XxxDaoImpl.class.getDeclaredMethods();
        for (Method method : declaredMethods1) {
            System.out.println(method.getName() + "  " + method.isDefault());
        }
    }

    @Test
    public void test3() {
        HashMap<String, Integer> map = new HashMap<>();
        Integer abc = map.computeIfAbsent("abc", x -> 1);
        System.out.println(abc);
        Integer abc1 = map.computeIfAbsent("abc", x -> 2);
        System.out.println(abc1);
        System.out.println();
    }

    @Test
    public void test4() {
        HashMap<String, Integer> map = new HashMap<>();
        map.compute("1", (k, v)-> (v == null?0:v) + 1);
        System.out.println();
        map.compute("1", (k, v)-> (v == null?0:v) + 1);
        System.out.println();

    }
    public static void main(String[] args) {
//        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DBConfig.class);
//        ReceiveAddressService bean = context.getBean(ReceiveAddressService.class);
//        Map<String, Object> receiveOrder = bean.getReceiveOrder(1);
//        System.out.println(receiveOrder);
        System.out.println("".length());
    }
}
